package com.bigknow.appstore.web.base.controller;

import com.bigknow.appstore.web.app.service.impl.AppStoreService;
import com.bigknow.appstore.web.base.dao.IUserDao;
import com.bigknow.appstore.web.base.service.ICommentService;
import com.bigknow.appstore.web.base.service.impl.CommentService;
import com.bigknow.appstore.web.console.app.Comment;
import com.bigknow.appstore.web.framework.controller.AbstractRestController;
import com.bigknow.appstore.web.framework.util.GlobalConstant;
import com.bigknow.appstore.web.framework.vo.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/1.
 */
@Controller
@RequestMapping(value = "/app/comment")
public class CommentController extends AbstractRestController<Comment, ICommentService> {
    @Autowired
    private IUserDao userDao;

    @Autowired
    private CommentService commentService;

    @Autowired
    AppStoreService appStoreService;

    @Override
    protected String getBasePath() {
        return "app";
    }

    @Override
    protected String getBaseName() {
        return "comment";
    }

    @RequestMapping("/submit")
    @ResponseBody
    public boolean submit(String appId, float score, String content, boolean anonymous) {
        commentService.insertComment(appId, score, content, anonymous);
        return true;
    }


    @RequestMapping(value = "/list/{appId}")
    public ModelAndView list(Comment comment, @PathVariable String appId, Integer pageIndex, Integer pageSize) {
        Map<String, Object> map = new HashMap<String, Object>();
        comment.setForStat(false);
        try {
            pageIndex = pageIndex == null ? GlobalConstant.DEFAULT_PAGE_INDEX : pageIndex;
            pageSize = pageSize == null ? GlobalConstant.DEFAULT_PAGE_SIZE : pageSize;
            comment.setOrderBy("-updateTime");
            switch (getSearchPattern()) {
                default:
                    map.put("page", this.service.find(comment, pageIndex, pageSize));
                    map.put("fuzzyField", "");
                    map.put("record", comment);
                    break;
            }
        } catch (Exception e) {
            this.logger.warn("List page error.", e);
            e.printStackTrace();
        }

        ((Page<Comment>) map.get("page")).getEntities().forEach(x -> loadRef(x));
        ModelAndView view = new ModelAndView("app/comment/commentList", map);
        view.addObject("isLogin", appStoreService.isLogin());
        view.addObject("appId", appId);
        return view;
    }

    private void loadRef(Comment comment) {
        if(!StringUtils.isEmpty(comment.getUserId())){
            comment.setUserName(userDao.get(comment.getUserId()).getName());
        }
    }

}
